<?php
/**
 * This file is part of Task Agile.
 * Task Agile is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License.
 * Task Agile is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Task Agile.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category   TaskAgile
 * @package    Controller
 * @copyright  Copyright (c) 20010-2010 Estrada Virtual (http://estradavirtual.com.br)
 * @license    http://www.gnu.org/licenses/gpl.txt GPLv3
 * @version    $Id$
*/
class Task_CronController extends Tev_Controller_Action
{
    public function indexAction()
    {
        $this->_execute();
        die;
    }

    private function _execute()
    {
        $hour = date('H');
        if (8 < $hour && $hour < 18) {
            $this->_sendEmailTaskOpened();
        }
    }

    private function _sendEmailTaskOpened()
    {
        $cron = new Cron();
        if ($cron->fetchAll("dt_register = '" . date('Y-m-d') . "'", 1)->count()) {
            return false;
        }

        $task = new Task();
        $this->view->prioritys       = $task->getPrioritys();
        unset($task);

        // Projects
        $project = new Project();
        $rowSetProjects = $project->fetchAll(null, 'name');
        unset($project);
        $projects = array();
        foreach ($rowSetProjects as $rowProject) {
            $projects[$rowProject->id] = $rowProject->name;
        }
        $this->view->projects = $projects;
        unset($projects);

        // Email
        $mail = new Zend_Mail('UTF8');
        $translate = Zend_Registry::get('Zend_Translate');
        $mail->setSubject($translate->_('email-subject-task-list'));

        // Users
        $user = new User();
        $rowSetUsers = $user->fetchAll();

        foreach ($rowSetUsers as $rowUser) {
            $selectTask = $user->select(false)->where('status = ?', Task::STATUS_ACTIVE)->order(array('fk_project', 'priority', 'name'));
            $rowSetTasks = $rowUser->findTask($selectTask);
            if (count($rowSetTasks) < 1) {
                continue;
            }
            $this->view->rowUser     = $rowUser;
            $this->view->rowSetTasks = $rowSetTasks;

            $emailBody = $this->view->render('email/task-list.phtml');
            $mail->setBodyHtml($emailBody);
            $mail->clearRecipients();
            $mail->addTo($rowUser->email, $rowUser->name);
            try {
                $mail->send();
            } catch (Exception $e) {
                // @todo tratar quando não conseguir enviar email
            }
        }
        $cron->insert(array('code' => Cron::CODE_SEND_MAIL_TASK_LIST, 'dt_register' => date('Y-m-d')));
        return true;
    }
}
